package models

import (
	"github.com/astaxie/beego/orm"
	"strconv"
	"time"
)

type ArticleModels struct{}

type Article struct {
	Id          int64  `orm:pk;auto;size(11)`
	Title       string `orm:size(200)`
	Type_id     int
	Picture     string
	Keywords    string
	Desc        string
	Content     string
	Sort        int
	Add_time    int64
	Update_time int64
	Fromlink    string
	Is_open     int    `orm:size(1);default(1)`
	Type_name   string `orm:"-"`
}

func init() {
	orm.RegisterModel(new(Article))
}

func (a *ArticleModels) Get(id int64) (Article, error) {
	article := Article{Id: id}
	o := orm.NewOrm()
	err := o.Read(&article)
	if err == nil {
		at := ArticleTypeModels{}
		id := int64(article.Type_id)
		ats, _ := at.Get(id)
		article.Type_name = ats.Title
	}
	return article, err
}

func (a *ArticleModels) QueryByPage(maps map[string]interface{}) ([]Article, int64, error) {
	var list []Article
	ps := getQueryTable("article")

	if maps["page"] == nil {
		maps["page"] = 1
	}
	if maps["limit"] == nil {
		maps["limit"] = 10
	}
	if maps["type_id"] != nil {
		ps = ps.Filter("type_id", maps["type_id"])
	}
	if maps["title"] != nil {
		ps = ps.Filter("title__icontains", maps["title"])
	}
	if maps["keywords"] != nil {
		ps = ps.Filter("keywords__icontains", maps["keywords"])
	}
	page, _ := maps["page"].(int)
	limit, _ := maps["limit"].(int)
	off := (page - 1) * limit
	_, err := ps.Limit(limit, off).All(&list)
	at := ArticleTypeModels{}
	for i := 0; i < len(list); i++ {
		id := int64(list[i].Type_id)
		ats, _ := at.Get(id)
		list[i].Type_name = ats.Title
	}
	count, _ := ps.Count()
	return list, count, err
}

func (a *ArticleModels) Add(maps map[string]interface{}) (int64, error) {
	article := Article{}
	if maps["title"] != nil {
		article.Title = maps["title"].(string)
	}
	if maps["type_id"] != nil {
		article.Type_id, _ = strconv.Atoi(maps["type_id"].(string))
	}
	if maps["picture"] != nil {
		article.Picture = maps["picture"].(string)
	}
	if maps["keywords"] != nil {
		article.Keywords = maps["keywords"].(string)
	}
	if maps["desc"] != nil {
		article.Desc = maps["desc"].(string)
	}
	if maps["content"] != nil {
		article.Content = maps["content"].(string)
	}
	if maps["fromlink"] != nil {
		article.Fromlink = maps["fromlink"].(string)
	}
	if maps["sort"] != nil {
		article.Sort, _ = strconv.Atoi(maps["sort"].(string))
	}
	if maps["is_open"] != nil {
		article.Is_open, _ = strconv.Atoi(maps["is_open"].(string))
	}
	article.Add_time = time.Now().Unix()
	article.Update_time = article.Add_time
	o := orm.NewOrm()
	return o.Insert(&article)
}

func (a *ArticleModels) Update(maps map[string]interface{}) (int64, error) {
	article := Article{}
	article.Id, _ = strconv.ParseInt(maps["id"].(string), 10, 64)

	o := orm.NewOrm()
	_ = o.Read(&article)

	if maps["title"] != nil {
		article.Title = maps["title"].(string)
	}
	if maps["type_id"] != nil {
		article.Type_id, _ = strconv.Atoi(maps["type_id"].(string))
	}
	if maps["picture"] != nil {
		article.Picture = maps["picture"].(string)
	}
	if maps["keywords"] != nil {
		article.Keywords = maps["keywords"].(string)
	}
	if maps["desc"] != nil {
		article.Desc = maps["desc"].(string)
	}
	if maps["content"] != nil {
		article.Content = maps["content"].(string)
	}
	if maps["fromlink"] != nil {
		article.Fromlink = maps["fromlink"].(string)
	}
	if maps["sort"] != nil {
		article.Sort, _ = strconv.Atoi(maps["sort"].(string))
	}
	if maps["is_open"] != nil {
		article.Is_open, _ = strconv.Atoi(maps["is_open"].(string))
	}
	article.Update_time = time.Now().Unix()

	return o.Update(&article)
}

func (a *ArticleModels) Delete(id int64) (int64, error) {
	article := Article{Id: id}
	o := orm.NewOrm()
	return o.Delete(&article)
}
